package com.central.apps.handler.purchase;

import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.apps.service.CostOccupyService;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 化学品采购申请
 *
 * @author JHY
 * @date 2023/05/15
 */
@Component
@Slf4j
public class ChemistryPurchRequtionHandler extends LvBaseHandler {


    @Autowired
    private HandlerService handlerService;
    @Autowired
    private CostOccupyService costOccupyService;

    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {
        System.out.println(" ===> enter ChemistryPurchRequtionHandler beforeExcuteFlow");
        System.out.println(" ===> app:" + app);
        System.out.println(" ===> id:" + id);
        System.out.println(" ===> flowNode:" + flowNode);
        System.out.println(" ===> op:" + op);
        return null;
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        System.out.println(" ===> enter ChemistryPurchRequtionHandler afterSave");
        System.out.println(" ===> app:" + app);
        System.out.println(" ===> objName:" + objName);
        System.out.println(" ===> data:" + data);
        System.out.println(" ===> isUpdate:" + isUpdate);
        if ("pr2App".equals(app)) {
            if ("gq_pr".equals(objName)) {
                String id = MapUtils.getString(data, "id");
                // type=3 化学品采购申请
                handlerService.updateBySql("update gq_pr set type=3 where id='" + id + "'");
                System.out.println(" ===> 修改申请类型=3");
            }else if ("gq_prline".equals(objName)) {
                String prid = MapUtils.getString(data, "prid");
                String prnum = MapUtils.getString(data, "prnum");
                handlerService.updateBySql("update gq_pr set totalcost=0 where prnum='" + prnum + "' and id='" + prid + "'");
                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_pr where prnum='" + prnum + "'  and id='" + prid + "'");
                if (CollectionUtils.isNotEmpty(bySql)) {
                    Map<String, Object> map = bySql.get(0);
                    String totalcost = MapUtils.getString(map, "totalcost");
                    String subjectno = MapUtils.getString(map, "subjectno");//课题号
                    String applyby = MapUtils.getString(map, "applyby");//需求人
                    BigDecimal bigDecimal = new BigDecimal(totalcost);//总计金额
                    String proid = MapUtils.getString(map, "proid");
                    handlerService.updateBySql("update gq_pr set totalcost=0 where prnum='" + prnum + "' and id='" + prid + "'");
                    //handlerService.updateBySql("update gq_prline set subjectno='" + subjectno + "',applyby='" + applyby + "' where prid='" + prid + "'");
                    handlerService.updateBySql("update gq_prline set subjectno='"+subjectno+"',proid = '"+proid+"',applyby='"+applyby+"' where prid='"+prid+"'");
                    List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_prline where prnum='" + prnum + "' and prid='" + prid + "'");
                    if (CollectionUtils.isNotEmpty(bySql1)) {
                        for (int i = 0; i < bySql1.size(); i++) {
                            Map<String, Object> map1 = bySql1.get(i);
                            String linecost = MapUtils.getString(map1, "linecost");
                            BigDecimal bigDecimal1 = new BigDecimal(linecost);
                            bigDecimal = bigDecimal.add(bigDecimal1);
                        }
                        handlerService.updateBySql("update gq_pr set totalcost='" + bigDecimal + "' where prnum='" + prnum + "' and id='" + prid + "'");
                    }
                    List<Map<String, Object>> bySql2 = handlerService.findBySql("select * from gq_prline where prid=" + prid);
                    if (CollectionUtils.isNotEmpty(bySql2)){
                        float A1totalcost =0;
                        float A4totalcost =0;
                        float A5totalcost =0;
                        float A2totalcost=0;
                        for(int i=0;i<bySql1.size();i++){
                            Map<String, Object> map1 = bySql2.get(i);
                            String itemtype = MapUtils.getString(map1, "itemtype");
                            Float linecost = MapUtils.getFloat(map1, "linecost");
                            if (itemtype.contains("A1类")){
                                A1totalcost=A1totalcost+linecost;
                            }else if (itemtype.contains("A4类")){
                                A4totalcost=A4totalcost+linecost;
                            }else if (itemtype.contains("A5类")){
                                A5totalcost = A5totalcost+linecost;
                            }else if (itemtype.contains("A2类")){
                                A2totalcost =A2totalcost+linecost;
                            }
                        }
                        handlerService.updateBySql("update gq_pr set A1totalcost="+A1totalcost+",A4totalcost="+A4totalcost+",A5totalcost="+A5totalcost+",A2totalcost="+A2totalcost+" where id = " + prid );
                    }
                }
            }
        }
        super.afterSave(app, objName, data, isUpdate);
    }


    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        System.out.println(" ===> enter ChemistryPurchRequtionHandler operate");
        System.out.println(" ===> app:" + app);
        System.out.println(" ===> type:" + type);
        System.out.println(" ===> params:" + params);
        return super.operate(app, type, params);
    }
}
